<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { productTypeArr } from '@/pinia/getter';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import serachUserid from '@/components/serachUserid/index.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    goodsList:[] as Array<any>,
    goodsSKU:[] as Array<any>,
  })
  interface formQuert {
    uuid:number
    user_id:number | string
    type:number
    goods_id:number
    sku_id:number
    sys_notice:string
    desc:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [{ required: true, message: "请输入用户UUID", trigger: "blur" }],
    type: [{ required: true, message: "请选择商品类型", trigger: "blur" }],
    goods_id: [{ required: true, message: "请选择商品", trigger: "blur" }],
    sku_id: [{ required: true, message: "请选择商品SKU", trigger: "blur" }],
    sys_notice: [{ required: true, message: "请输入系统通知", trigger: "blur" }],
  }
  const getGoodsList = async(goodsType:number = 0)=>{
    if(!goodsType) return
    const res = await propApi.getTypegoodsList(goodsType)
    const { data=[] } = res
    action.goodsList = data
  }
  const getGoodsSkuList = async(goods_id:number = 0)=>{
    if(!goods_id) return
    const res = await propApi.getGoodsSkuList(goods_id)
    const { data } = res
    action.goodsSKU = data
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      form.user_id = JSON.stringify(form.user_id) // api接口校验位字符串类型
      await propApi.addSendGoodsRecord(form).then(()=>{
        ToastSuccess('赠送成功')
        loading.close()
        closeDialog()
        $emit('updateList')
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = async(query:any = {})=> {
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      title="赠送商品">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="用户UUID" prop="user_id">
          <serachUserid v-model:uuid="form.uuid" v-model:user_id="form.user_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="商品类型" prop="type">
          <el-select v-model="form.type" @change="getGoodsList" placeholder="请选择商品类型">
            <el-option
              v-for="item in productTypeArr"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="商品" prop="goods_id">
          <el-select filterable v-model="form.goods_id" @change="getGoodsSkuList" placeholder="请选择商品">
            <el-option
              v-for="item in action.goodsList"
              :label="item.goods_name"
              :value="item.goods_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="商品SKU" prop="sku_id">
          <el-select v-model="form.sku_id" placeholder="请选择商品SKU">
            <el-option
              v-for="item in action.goodsSKU"
              :label="`${item.time_limit}天 ${item.price}钻石`"
              :value="item.sku_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="系统通知" prop="sys_notice">
          <el-input
            v-model="form.sys_notice"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入系统通知"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="说明">
          <el-input
            v-model="form.desc"
            :autosize="{ minRows: 3, maxRows: 3}"
            type="textarea"
            placeholder="请输入说明"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>